package com.nineclock.system.controller;

import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.OrganizationControllerApi;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {
    @Autowired
    private SysCompanyUserService sysCompanyUserService;
    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> companyUserDTOS = sysCompanyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOS);
    }


    @Autowired
    private SysDepartmentService departmentService;
    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> sysDepartmentDTOS = departmentService.queryDepartmentList();
        return Result.success(sysDepartmentDTOS);
    }

    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                         @RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "departmentId", required = false) Long departmentId) {
        PageResult<SysCompanyUserDTO> pageResult = sysCompanyUserService.members(page,pageSize,keyword,departmentId);
        return Result.success(pageResult);
    }

    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        sysCompanyUserService.parseExcel(excelFile);
        return Result.success();
    }

    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds(){
        List<Long> ids = departmentService.queryTopDepartmentIds();
        return Result.success(ids);
    }

    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = sysCompanyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }

    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
            @RequestParam(value = "id", required = false) Long departmentId,
            @RequestParam(value = "includeMember", required = false) Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> userSimpleDTOS = departmentService.querySimpleDepartment(departmentId, includeMember);
        return Result.success(userSimpleDTOS);
    }

}
